package cn.ztydata.teachingsystem.heaven.dao.redis;


import org.springframework.data.redis.core.ListOperations;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 习题->子习题 Redis类
 * Created by chenxin on 2015/02/04.
 */
@Repository
public class ExerciseRelationDao extends BaseRedisDao {
    private static final String PARENT_SON = "exercise:p_s";

    /**
     * 保存子习题ID
     * @param exerciseId 习题ID
     * @param sonId 子习题ID
     *
     * @author chenxin
     * @since 2015/02/04
     */
    public void saveSonExercise(final long exerciseId, final long sonId) {
        //获取list操作
        ListOperations<String, Long> listOperations = redisTemplate.opsForList();

        listOperations.rightPush(KeyUtils.generateKey(PARENT_SON, exerciseId), sonId);
    }

    /**
     * 获取所有子习题ID
     * @param exerciseId 习题ID
     *
     * @author chenxin
     * @since 2015/02/04
     */
    public List<Long> getSonIds(final long exerciseId) {
        //获取list操作
        ListOperations<String, Long> listOperations = redisTemplate.opsForList();

        logger.debug("获取所有子习题ID");
        return listOperations.range(KeyUtils.generateKey(PARENT_SON, exerciseId), 0, -1);
    }
}
